/*
 * Copyright (c) 2012 SUBSECRETARIA DEFENSORIA DEL ESPACIO PUBLICO - SDEP.
 * All Rights Reserved.
 * This software is the confidential and proprietary information of SDEP
 * Developed By CONSTULT-SOFT S.A, Development Team 2012.
 *
 */
package co.gov.medellin.sdep.sisdep.domain.gestion;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import co.gov.medellin.sdep.sisdep.common.AppConstants;
import co.gov.medellin.sdep.sisdep.domain.MessageModalWindowController;
import co.gov.medellin.sdep.sisdep.domain.SolicitudBaseController;
import co.gov.medellin.sdep.sisdep.gestion.exception.ConceptoInicialException;
import co.gov.medellin.sdep.sisdep.gestion.exception.ConceptoInicialRuntimeException;
import co.gov.medellin.sdep.sisdep.gestion.exception.SolicitudException;
import co.gov.medellin.sdep.sisdep.gestion.service.IConceptoInicialService;
import co.gov.medellin.sdep.sisdep.model.ConceptoInicial;
import co.gov.medellin.sdep.sisdep.regulacion.exception.SolicitudModificaException;

/**
 * @author Usuario
 * 
 */
public class ConceptoInicialController extends SolicitudBaseController implements
		Serializable {

	/**
	 * generated serial version
	 */
	private static final long serialVersionUID = -3285110767384990403L;

	/**
	 * Constructor por defecto, inicializa los elemento basicos del Controlador
	 */
	public ConceptoInicialController() {
		cleanSearchValues();
		cleanValues();
	}

	/** bean para el manejo del formulario de busqueda ConceptoInicial */
	public ConceptoInicial conceptoInicialSearch = new ConceptoInicial();

	/** Objeto contenedor del listado de ConceptoInicial luego de una consulta. */
	private DataModel conceptoInicialListModel;

	/** bean para el manejo del formulario de creacion/edicion. ConceptoInicial */
	public ConceptoInicial conceptoInicial = new ConceptoInicial();

	/** instancia de servicio conceptoInicialService */
	private IConceptoInicialService conceptoInicialService;

    // --------------------------------------------------------------------
	// --- metodos para atender acciones init()
	// --------------------------------------------------------------------

	/**
	 * 
	 * @return String
	 * @throws SolicitudException 
	 * @throws SolicitudModificaException 
	 */
	public String initState() throws SolicitudException, ConceptoInicialException {
		Map params = getExternalContext().getRequestParameterMap();
		Long solicitudId = new Long((String) params.get("solicitudId"));
		this.solicitudOrigenRedirectPage = (String) params.get("solicitudOrigenRedirectPage");
		this.solicitudDestinoRedirectPage = (String) params.get("solicitudDestinoRedirectPage");
		cleanSearchValues();
		this.solicitud = getSolicitudService().get(solicitudId.longValue());
		this.conceptoInicial = getConceptoInicialService().getConceptoInicialRelatedBy(solicitudId.longValue());
		if(this.conceptoInicial == null){
			this.conceptoInicial = new ConceptoInicial();
			cleanValues();
			this.conceptoInicial.setIdSolicitud(solicitudId);
			return solicitudOrigenRedirectPage;
		}
		return solicitudOrigenRedirectPage;
	}
	
	/**
	 * 
	 * @return String
	 */
	public String searchConceptoInicialByKey() {
		String msg = "";
		try {
			List valores = null;
			valores = getConceptoInicialService().loadByRadicado(1, AppConstants.PAGE_SIZE,
					this.conceptoInicialSearch);
			this.conceptoInicialListModel.setWrappedData(valores);
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.conceptoInicialListModel.getWrappedData() == null) {
			msg = AppConstants.NO_RECORDS_FOUND;
		} else {
			if (((List) this.conceptoInicialListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(msg, null, null);
		return AppConstants.CONCEPTO_INICIAL_SEARCH_VIEW;
	}
	
	public String searchConceptoInicialByKey(String radicado,IConceptoInicialService service) {
		String msg = "";
		try {
			List valores = null;
			ConceptoInicial concepto = new ConceptoInicial();
			concepto.setRadicado(radicado);
			this.setConceptoInicialService(service);
			valores = getConceptoInicialService().loadByRadicado(1, AppConstants.PAGE_SIZE,
					this.conceptoInicialSearch);
			this.conceptoInicialListModel.setWrappedData(valores);
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.conceptoInicialListModel.getWrappedData() == null) {
			msg = AppConstants.NO_RECORDS_FOUND;
		} else {
			if (((List) this.conceptoInicialListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
	//	((MessageModalWindowController) getBean("messageController"))
	//			.setSuccessMessage(msg, null, null);
		return AppConstants.CONCEPTO_INICIAL_SEARCH_VIEW;
	}

	public int searchConceptoInicialByIdSolicitud(Long idSolicitud,IConceptoInicialService service) {
		String msg = "";
		int intRdo = 0;
		try {
			List valores = null;
			ConceptoInicial concepto = new ConceptoInicial();
			concepto.setIdSolicitud(idSolicitud);
			this.setConceptoInicialService(service);
			valores = getConceptoInicialService().loadByIdSolicitud(1, AppConstants.PAGE_SIZE,
					concepto);
			this.conceptoInicialListModel.setWrappedData(valores);
			intRdo = valores.size();
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.conceptoInicialListModel.getWrappedData() == null) {
			msg = AppConstants.NO_RECORDS_FOUND;
		} else {
			if (((List) this.conceptoInicialListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(msg, null, null);
		return intRdo;
	}	
	
	/**
	 * 
	 * @return
	 */
	public String create() {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (this.conceptoInicial.getId().longValue() == 0L) {
				conceptoInicial.setIdContravencion(new Long(conceptoInicial.getIdContravencionStr()));
				getConceptoInicialService().insert(this.conceptoInicial);
				msg = AppConstants.SUCCESS_CREATE;
			}
		} catch (ConceptoInicialException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (ConceptoInicialRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		cleanValues();
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(" Concepto Inicial " + msg, null, null);
		return this.solicitudDestinoRedirectPage;
	}

	/**
	 * 
	 * @return
	 */
	public String update() {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (this.conceptoInicial.getId().longValue() == 0L) {
				conceptoInicial.setIdContravencion(new Long(conceptoInicial.getIdContravencionStr()));
				getConceptoInicialService().insert(this.conceptoInicial);
				msg = AppConstants.SUCCESS_CREATE;
			}
			if (this.conceptoInicial.getId().longValue() != 0L) {
				conceptoInicial.setIdContravencion(new Long(conceptoInicial.getIdContravencionStr()));
				getConceptoInicialService().update(this.conceptoInicial);
				msg = AppConstants.SUCCESS_UPDATE;
			}
		} catch (ConceptoInicialException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (ConceptoInicialRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		cleanValues();
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(" Concepto Inicial " + msg, null, null);
		return this.solicitudDestinoRedirectPage;
	}

	// --------------------------------------------------------------------
	// --- metodos para atender acciones end()
	// --------------------------------------------------------------------

	// --------------------------------------------------------------------
	// --- metodos para atender reglas de navegacion init()
	// --------------------------------------------------------------------

	/**
	 * 
	 */
	public String createConceptoInicial() {
		conceptoInicial = new ConceptoInicial();
		conceptoInicial.setId(new Long(0L));
		conceptoInicial.setIdSolicitud(conceptoInicialSearch.getIdSolicitud());
		//conceptoInicial.setCodigo("");
		//conceptoInicial.setValor("");
		//conceptoInicial.setActivo("N");
	    this.solicitudDestinoRedirectPage = AppConstants.CONCEPTO_INICIAL_SEARCH_VIEW;
		return AppConstants.CONCEPTO_INICIAL_CREATE_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String updateConceptoInicial() {
		Map params = getExternalContext().getRequestParameterMap();
		String conceptoInicialDataIndex = (String) params
				.get("conceptoInicialDataIndex");
		if(conceptoInicialDataIndex == null)
			conceptoInicialDataIndex = "0";
		
		this.conceptoInicial = (ConceptoInicial) ((List) this.conceptoInicialListModel
				.getWrappedData())
				.get(Integer.parseInt(conceptoInicialDataIndex));
	    this.solicitudDestinoRedirectPage = AppConstants.CONCEPTO_INICIAL_SEARCH_VIEW;
		return AppConstants.CONCEPTO_INICIAL_UPDATE_VIEW;
	}

	/**
	 * limpia el contenido de los valores que se ingresan para buscar
	 */
	public final void cleanSearchValues(){
		conceptoInicialSearch.setIdSolicitud(new Long(0L));
		conceptoInicialSearch.setEstado("");
	}

	/**
	 * limpia el contenido de los valores que se ingresan (Create, Update)
	 */
	public final void cleanValues() {
		conceptoInicial.setId(new Long(0L));
		conceptoInicial.setIdSolicitud(new Long(0L));
		conceptoInicial.setIdContravencion(new Long(0L));
		conceptoInicial.setDescripcion("");
		conceptoInicial.setObservacion("");
		conceptoInicial.setEstado("");
		conceptoInicial.setUsuarioCrea("");
		conceptoInicial.setFechaCrea(null);
		conceptoInicial.setUsuarioModifica("");
		conceptoInicial.setFechaModifica(null);
		this.conceptoInicialListModel = new ListDataModel();
	}

	/**
	 * 
	 * @return
	 */
	public String cancelConceptoInicial() {
		return AppConstants.CONCEPTO_INICIAL_SEARCH_VIEW;
	}

	
	/**
	 * 
	 * @return
	 */
	public String cancelConceptoInicialOpcion() {
		return AppConstants.CONCEPTO_INICIAL_MENU_VIEW;
	}
	// --------------------------------------------------------------------
	// --- metodos para atender reglas de navegacion end()
	// --------------------------------------------------------------------

	// --------------------------------------------------------------------
	// --- getters and setters init()
	// --------------------------------------------------------------------

	/**
	 * 
	 */
	public ConceptoInicial getConceptoInicialSearch() {
		return conceptoInicialSearch;
	}

	/**
	 * 
	 * @param conceptoInicialSearch
	 */
	public void setconceptoInicialSearch(ConceptoInicial conceptoInicialSearch) {
		this.conceptoInicialSearch = conceptoInicialSearch;
	}

	/**
	 * 
	 * @return
	 */
	public DataModel getconceptoInicialListModel() {
		return conceptoInicialListModel;
	}

	/**
	 * 
	 * @param conceptoInicialListModel
	 */
	public void setconceptoInicialListModel(DataModel conceptoInicialListModel) {
		this.conceptoInicialListModel = conceptoInicialListModel;
	}

	/**
	 * 
	 * @return
	 */
	public ConceptoInicial getConceptoInicial() {
		return conceptoInicial;
	}

	/**
	 * 
	 * @param conceptoInicial
	 */
	public void setconceptoInicial(ConceptoInicial conceptoInicial) {
		this.conceptoInicial = conceptoInicial;
	}

	/**
	 * obtencion del servicio conceptoInicialService
	 * 
	 * @return
	 */
	public IConceptoInicialService getConceptoInicialService() {
		return conceptoInicialService;
	}

	/**
	 * establecimiento del servicio conceptoInicialService, la inyeccion de la
	 * dependencia la cubre SpringFramework
	 * 
	 * @param conceptoInicialService
	 */
	public void setConceptoInicialService(
			IConceptoInicialService conceptoInicialService) {
		this.conceptoInicialService = conceptoInicialService;
	}

	// --------------------------------------------------------------------
	// --- getters and setters end()
	// --------------------------------------------------------------------

}
